<template>
  <div>
    <NavBarVue title="商品列表" />
    <div class="order-bar">
      <div @click="handleOrder(0)" :class="['order', 'sale', {
       active: orderStatus.activeIndex === 0
      }]">
        <span>销量</span>
      </div>
      <div @click="handleOrderPrice" :class="['order', 'price', {
        active: orderStatus.activeIndex === 1
      }]">
        <span>价格</span>
        <div :class="['arrow', 'up', {
          active: orderStatus.activeIndex === 1 && orderStatus.upDown === 1
        }]"></div>
        <div :class="['arrow', 'bottom', {
          active: orderStatus.activeIndex === 1 && orderStatus.upDown === 0
        }]"></div>
      </div>
      <div @click="handleOrder(2)" :class="['order', 'new', {
        active: orderStatus.activeIndex === 2
      }]">
        <span>新品</span>
      </div>
    </div>
    <div class="content">
      <div class="items">
        <div class="item" @click="enterDetail(item.id)" v-for="item in items" :key="item.id">
          <img :src="item.pic" class="item-img" alt="">
          <div class="item-info">
            <h5>{{ item.name }}</h5>
            <div class="item-action">
              <p>￥{{ item.minPrice }}</p>
              <van-icon size="26" class-prefix="icon" name="gouwuchetianjia" />
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts" name="ItemLists">
import NavBarVue from '@/components/NavBar.vue';
import { ref, reactive, computed, onMounted } from 'vue'
import { fetchItems } from '@a/index'
import { useRoute, useRouter } from 'vue-router';
const route = useRoute();
const router = useRouter();
const enterDetail = (id:number) => {
  router.push({
    path: '/detail',
    query: {
      id
    }
  })
}
// 控制 排序栏 排序规则
const orderStatus = reactive({
  activeIndex: 0, // 0销量 1 价格 2 新品
  upDown: 0 // 0降序 1 升序
})
// 商品列表排序的参数
const order = computed(() => {
  let orderStr = 'ordersDown'
  // 判断
  switch (orderStatus.activeIndex) {
    case 0:
      // 销量排序
      orderStr = 'ordersDown'
      break;
    case 1:
      // 按照价格
      if (orderStatus.upDown === 0) {
        // 价格降序
        orderStr = 'priceDown'
      } else {
        // 价格升序
        orderStr = 'priceUp'
      }
      break;
    case 2:
      // 按照新品排序
      orderStr = 'addedDown'
      break;
  }
  return orderStr
})

// 点击排序栏排序 重新请求数据
const handleOrder = (activeIndex: number) => {
  // 按照销量和新品排序
  orderStatus.activeIndex = activeIndex
  fetchListsItems()
}
// 按照价格排序
const handleOrderPrice = () => {
  /* 
    情况1 
      此时 没有按照价格排序 
        点击 按照价格 的 升序
    情况2 
      此时就是按照价格 排序 点击 切换升序和降序
  */
  if (orderStatus.activeIndex !== 1) {
    // 按照价格 升序排序
    orderStatus.activeIndex = 1
    orderStatus.upDown = 1
  } else {
    // 价格升序降序 之间 切换
    if (orderStatus.upDown === 1) {
      orderStatus.upDown = 0
    } else {
      orderStatus.upDown = 1
    }
  }
  fetchListsItems()
}

// 请求商品列表
const items = ref<any[]>([]);
const fetchListsItems = () => {
  fetchItems({
    categoryId: route.query.cateId,
    orderBy: order.value
  }).then(res => {
    if (res.data.code === 0) {
      items.value = res.data.data.result
    }
  })
}

onMounted(() => {
  fetchListsItems()
})
</script>

<style scoped lang="scss">
.order-bar {
  height: 40px;
  width: 100%;
  position: fixed;
  left: 0;
  top: 46px;
  background-color: #fff;
  z-index: 10;
}

.order-bar {
  display: flex;
  font-size: 14PX;
  color: #333;
  align-items: center;
  justify-content: space-around;

  .order.active {
    color: #e93323;
  }

  .order.price {
    position: relative;

    .arrow {
      width: 0;
      height: 0;
      border: 5px solid transparent;
      position: absolute;
      right: -12PX;

      &.up {
        border-bottom-color: #333;
        top: 2px;

        &.active {
          border-bottom-color: #e93323;
        }
      }

      &.bottom {
        border-top-color: #333;
        top: 14PX;

        &.active {
          border-top-color: #e93323;
        }
      }
    }
  }
}
.content{
  padding-top: 86px;
}
.item {
    display: flex;
    padding: 10px;
    background-color: #fff;
    margin-top: 10px;

    img {
      width: 120px;
    }

    .item-info {
      flex: 1;
      padding: 0 10px;

      h5 {
        line-height: 40px;
      }

      .item-action {
        display: flex;
        justify-content: space-between;

        p {
          font-size: 20PX;
          color: #ec1109;
        }

      }
    }
  }
</style>